package com.ycy.temperature.date.net;

import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;

import com.ycy.temperature.App;
import com.ycy.temperature.date.utils.SharedPreferencesUtils;

import java.io.IOException;

import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

/**
 * 访问接口
 */
public class LoggingInterceptor implements Interceptor {

    @Override
    public Response intercept(@Nullable Chain chain) throws IOException {
        Request request = chain.request();
        Request.Builder builder = request.newBuilder();
        if (!TextUtils.isEmpty(SharedPreferencesUtils.getString("token", "String", ""))) {
            builder.url(request.url().toString() + "?token=" + SharedPreferencesUtils.getString("token", "String", ""));
        } else {
            builder.url(request.url().toString());
        }

        Response resp = chain.proceed(builder.build());
        Log.i("resp.code()", resp.code() + "");
        if (resp.code() == 401) {
            App.getInstance().exitApp();
        }
        return resp;
    }
}
